using System;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;

namespace SPWebPartReplace
{
    public class ReportWebPartsReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                const string originalWebPartFullName = "Contoso.Portal.MyWebPart";
                SPSite site = (SPSite)properties.Feature.Parent;

                WebPart newWebPart = new Microsoft.SharePoint.WebControls.TopologyViewWebPart();
                using (WebPartReplace webpartRefresh = new WebPartReplace(site))
                {
                    webpartRefresh.ExecuteReplace(originalWebPartFullName, newWebPart);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                throw;
            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
        }
    }
}
